/**
 * 
 */
package com.zx.mes.upms.hyl.authenticaion;


import com.zx.mes.upms.hyl.entity.Menu;
import com.zx.mes.upms.hyl.entity.User;
import com.zx.mes.upms.hyl.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author zhailiang
 *
 */
@Component
@Transactional
public class RbacUserDetailsService implements UserDetailsService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserMapper userMapper;


	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		logger.info("表单登录用户名:" + username);
		User user = userMapper.getRoleAndResourceByUserName(username);
		user.getUrls();

		setMenuList(user,username);
		return user;
	}

	private void setMenuList(User user,String username) {
		User userMenu = userMapper.getUserAndRoleAndMenuByUserName(username);
		List<Menu> list = new ArrayList<>(50);
		HashMap<String, Menu> map = new HashMap<>(50);
		if (userMenu.getRoleList()!=null){
			for (int i=0;i<userMenu.getRoleList().size();i++){
				list.addAll(userMenu.getRoleList().get(i).getMenuList());
			}
			userMenu.setMenuList(list);
		}
		//去重复值 (利用hashMap来去重)
		for (int j=0;j<userMenu.getMenuList().size();j++){
			map.put(userMenu.getMenuList().get(j).getId(), userMenu.getMenuList().get(j));
		}

		List<Menu> menuList = new ArrayList<>(50);
		List<Menu> menuListParent = new ArrayList<>(50);
		List<Menu> menuListParentChildren = new ArrayList<>(50);
		Set<String> stringSet = map.keySet();
		for (String key:stringSet){
			menuList.add(map.get(key));
		}

		// 父子关系型展示
		for (int m=0;m<menuList.size();m++){
			if (menuList.get(m).getPid() == null || menuList.get(m).getPid().equalsIgnoreCase("")){
				menuListParent.add(menuList.get(m));
				menuListParentChildren.add(menuList.get(m));
			}
		}

		for (int n=0;n<menuListParent.size();n++){
			List<Menu> tempList = tempList=new ArrayList<>(10);
			for (int nn=0;nn<menuList.size();nn++){
				// menuList.get(nn).getPid().equalsIgnoreCase("") 防止pid是空字符串的情况
				if (menuList.get(nn).getPid() != null && !menuList.get(nn).getPid().equalsIgnoreCase("")) {
					if (menuListParent.get(n).getId().equals(menuList.get(nn).getPid())) {
						menuList.get(nn).setChildren(null);
						tempList.add(menuList.get(nn));
					}
				}
			}

			// 只要父菜单没有子菜单的都赋值为空
			if (tempList.size()==0){
				menuListParent.get(n).setChildren(null);
			}else{
				menuListParent.get(n).setChildren(tempList);
			}
		}


		userMenu.setMenuList(menuListParent);

		user.setMenuList(userMenu.getMenuList());
	}

}
